// JavaScript Document
//基地信息验证
function baseValidate() {
    var state = true;
    if ($.trim($("#baseName").val()) == '') {
        alert('请输入基地名称！');
        state = false;
        $('#baseName').focus();
    } else if ($("#tureName").val() == "" || $("#name").val() == "" || $("#password").val() == "" || $("#tel").val() == "" || $("#email").val() == "" || $("#password").val().length < 5 || (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($("#email").val()) == false)) {
        checkUserValidate();
        state = false;
    } else if ($.trim($("#affiliation").val()) == "") {
        alert('所属单位不能为空！');
        $('#affiliation').focus();
        state = false;
    } else if ($.trim($("#founded").val()) == "") {
        alert('成立时间不能为空！');
        $('#founded').focus();
        state = false;
    } else if ($.trim($("#founded").val()).length != 10 && $.trim($("#founded").val()).length != 11) {
        alert('时间格式有误！');
        $('#founded').focus();
        state = false;
    } else if ($.trim($("#content").val()) == "") {
        alert('基地简介不能为空！');
        $('#content').focus();
        state = false;
    }
    if (state) {
        $('form').submit();
    }
}
//区块信息验证
function blockValidate() {
    var state = true;
    if ($.trim($("#blockName").val()) == '') {
        alert('区块名称不能为空！');
        state = false;
        $('#blockName').focus();
    } else if ($("#tureName").val() == "" || $("#name").val() == "" || $("#password").val() == "" || $("#tel").val() == "" || $("#email").val() == "" || $("#password").val().length < 5 || (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($("#email").val()) == false)) {
        checkUserValidate();
        state = false;
    } else if ($.trim($("#address").val()) == "") {
        alert('区块地址不能为空！');
        $('#address').focus();
        state = false;
    }
    if (state) {
        $('form').submit();
    }
}
//团队表单验证
function teamValidate() {
    var state = true;
    if ($.trim($("#teamName").val()) == '') {
        alert('团队名称不能为空！');
        state = false;
        $('#teamName').focus();
    } else if ($("#tureName").val() == "" || $("#name").val() == "" || $("#password").val() == "" || $("#tel").val() == "" || $("#email").val() == "" || $.trim($("#password").val()).length < 5 || (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($("#email").val()) == false)) {
        checkUserValidate();
        state = false;
    } else if ($.trim($("#affiliation  option:selected").val()) == "none") {
        alert('请选择或填写单位信息！');
        $('#affiliation').focus();
        state = false;
    } else if ($.trim($("#teamtypeId  option:selected").val()) == '') {
        alert('请选择团队类型！');
        $('#teamtypeId').focus();
        state = false;
    } else if ($.trim($("#content").val()) == '') {
        alert('团队介绍不能为空！');
        $('#content').focus();
        state = false;
    } else if ($.trim($("#services").val()) == '') {
        alert('服务内容不能为空！');
        $('#services').focus();
        state = false;
    } else if ($.trim($("#joinRequires").val()) == '') {
        alert('报名条件不能为空！');
        $('#joinRequires').focus();
        state = false;
    }
    if (state) {
        $('form').submit();
    }
}
//用户表单信息验证
function checkUserValidate() {
    if ($.trim($("#name").val()) == '') {
        alert('用户名不能为空！');
        $('#name').focus();
    } else if (!/^[\u4E00-\u9FA5|a-zA-Z][\u4E00-\u9FA5|0-9a-zA-Z]*$/.test($("#name").val())) {
        alert('用户名只能为英文或者数字！');
        $('#name').focus();
    } else if ($.trim($("#password").val()) == "") {
        alert('密码不能为空！');
        $('#password').focus();
    } else if ($.trim($("#password").val()).length < 5) {
        alert('密码长度至少为6位有效数字！');
        $('#password').focus();
    } else if ($.trim($("#tel").val()) == "") {
        alert('联系电话不能为空！');
        $('#tel').focus();
    } else if ($.trim($("#email").val()) == "") {
        alert('邮箱不能为空！');
        $('#email').focus();
    } else if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($("#email").val()) == false) {
        alert('邮箱格式错误！');
        $('#email').focus();
    } else if ($.trim($("#tureName").val()) == "") {
        alert('真实姓名不能为空！');
        $('#tureName').focus();
    }
}
//志愿者表单验证
function userValidate() {
    var state = true;
    if ($.trim($.trim($("#name").val())) == "") {
        alert('志愿者姓名不能为空！');
        $('#name').focus();
        state = false;
    } else if ($.trim($("#email").val()) == "") {
        alert('邮箱不能为空！');
        $('#email').focus();
        state = false;
    } else if (!/.+@.+\.[a-zA-Z]{2,4}$/.test($("#email").val())) {
        alert('邮箱格式错误！');
        $('#email').focus();
		 state = false;
    } else if ($.trim($("#phone").val()) == "") {
        alert('手机号码不能为空！');
        $('#phone').focus();
        state = false;
    } else if ($.trim($("#idCard").val()) == "") {
        alert('身份证号码不能为空！');
        $('#idCard').focus();
        state = false;
    } else if (!/^(\d{15}|\d{17}[\dXx])$/.test($("#idCard").val())) {
        alert('身份证格式不正确！');
        $('#idCard').focus();
        state = false;
    } else if ($.trim($("#pwd").val()) == "") {
        alert('密码不能为空！');
        $('#phone').focus();
        state = false;
    }
    if (state) {
        $('form').submit();
    }
}

//专题表单验证
function specialValidate() {
    var state = true;
    if ($.trim($("#title").val()) == "") {
        alert('专题名称不能为空！');
        $('#title').focus();
        state = false;
    } else if ($.trim($("#copyFrom").val()) == "") {
        alert('专题来源不能为空！');
        $('#copyFrom').focus();
        state = false;
    } else if ($.trim($("#linkUrl").val()) == "") {
        alert('专题链接不能为空！');
        $('#linkUrl').focus();
        state = false;
    }
    if (state) {
        $('form').submit();
    }
}

//新闻表单验证
function newsValidate() {
    var state = true;
    if ($.trim($("#title").val()) == "") {
        alert('新闻标题不能为空！');
        $('#title').focus();
        state = false;
    } else if ($.trim($("#copyFrom").val()) == "") {
        alert('新闻来源不能为空！');
        $('#copyFrom').focus();
        state = false;
    } else if ($.trim($("#content").val()) == "") {
        alert('新闻内容不能为空！');
        $('#content').focus();
        state = false;
    }
    if (state) {
        $('form').submit();
    }
}
//风采录表单验证
function styleValidate() {
    var state = true;
    if ($.trim($("#title").val()) == "") {
        alert('标题不能为空！');
        $('#title').focus();
        state = false;
    } else if ($.trim($("#content").val()) == "") {
        alert('内容不能为空！');
        $('#content').focus();
        state = false;
    }

    if (state) {
        $('form').submit();
    }
}
//视频表单验证
function videoValidate(url) {
    var state = true;
    if ($.trim($("#title").val()) == "") {
        alert('标题不能为空！');
        $('#title').focus();
        state = false;
    } else if ($.trim($("#description").val()) == "") {
        alert('视频描述不能为空！');
        $('#description').focus();
        state = false;
    }

    if (state) {
        var bar = $('.bar');
        var percent = $('.percent');
        var showimg = $('#showimg');
        var progress = $(".progress");
        var files = $(".files");
        var btn = $(".btn span");
        var options = {
            dataType: 'json',
            type: 'POST',
            data: $('#myform').serialize(),
            url: url,
            beforeSend: function() {
                showimg.empty();
                progress.show();
                var percentVal = '0%';
                bar.width(percentVal);
                percent.html(percentVal);

            },
            uploadProgress: function(event, position, total, percentComplete) {
                var percentVal = percentComplete + '%';
                bar.width(percentVal);
                percent.html(percentVal);
            },
            success: function(data) {
                files.html();
                if (data.isShow == 2) {
                    progress.hide();
                }
                alert(data.msg);
                if (data.url) {
                    window.location.href = data.links;
                }

                showimg.html();
            },
            error: function(xhr) {
                alert(xhr.data);
            }
        };
        var form = $("#myform"); //form1:表单ID  在表单界面只用这一个表单ID
        form.ajaxSubmit(options);

    }
}

//资料表单验证
function fileValidate() {
    var state = true;
    if ($.trim($("#title").val()) == "") {
        alert('标题不能为空！');
        $('#title').focus();
        state = false;
    } else if ($.trim($("#videoFile").val()) == "") {
        alert('资料文件不能为空！');
        $('#videoFile').focus();
        state = false;
    }

    if (state) {
        var bar = $('.bar');
        var percent = $('.percent');
        var showimg = $('#showimg');
        var progress = $(".progress");
        var files = $(".files");
        var btn = $(".btn span");
        var options = {
            dataType: 'json',
            type: 'POST',
            data: $('#myform').serialize(),
            url: 'saveFileData',
            beforeSend: function() {
                showimg.empty();
                progress.show();
                var percentVal = '0%';
                bar.width(percentVal);
                percent.html(percentVal);

            },
            uploadProgress: function(event, position, total, percentComplete) {
                var percentVal = percentComplete + '%';
                bar.width(percentVal);
                percent.html(percentVal);
            },
            success: function(data) {
                files.html();
                if (data.isShow == 2) {
                    progress.hide();
                }
                alert(data.msg);
                if (data.url) {
                    window.location.href = "index";
                }

                showimg.html();
            },
            error: function(xhr) {
                alert(xhr.data);
            }
        };
        var form = $("#myform"); //form1:表单ID  在表单界面只用这一个表单ID
        form.ajaxSubmit(options);

    }
}
//友情链接表单验证
function friendLinkValidate() {
    var state = true;
    if ($.trim($("#linkName").val()) == "") {
        alert('标题不能为空！');
        $('#linkName').focus();
        state = false;
    } else if ($.trim($("#linkUrl").val()) == "") {
        alert('链接不能为空！');
        $('#linkUrl').focus();
        state = false;
    }

    if (state) {
        $('form').submit();
    }
}

//站内信全选反选
function  siteMailCheck(id,name){
 	if($('#'+id).is(':checked')) {
		$("input[name='"+name+"']").each(function() {
			$(this).attr("checked", true);
		});
		
	} else {
		$("input[name='"+name+"']").each(function() {
			$(this).attr("checked", false);
		});
	}	
	valueCheck();
	//getCookie();

}
//cookie触发
function cookieData(ckname){
	if('blockname'==ckname){$.cookies.set('blockCookie',null); }
	
	valueCheck();
	//getCookie();
	
}
//验证角色是否选中 提交信息   保存选中信息
function valueCheck(){
	var block_value = $.cookies.get( 'blockCookie' ) ?  $.cookies.get( 'blockCookie' ) : [];
	var base_value = $.cookies.get( 'baseCookie' ) ?  $.cookies.get( 'baseCookie' ) : [];
	var team_value = $.cookies.get( 'teamCookie' ) ?  $.cookies.get( 'teamCookie' ) : [];
	var user_value = $.cookies.get( 'userCookie' ) ? $.cookies.get( 'userCookie' ) : [];
	var super_value = $.cookies.get( 'superCookie' ) ?  $.cookies.get( 'superCookie' ) : [];
	$('input[name="blockname"]:checked').each(function() {
		block_value.push($(this).val());
	});
	$('input[name="basename"]:checked').each(function() {
		base_value.push($(this).val());
	});
	$('input[name="teamname"]:checked').each(function() {
		team_value.push($(this).val());
	});
	$('input[name="username"]:checked').each(function() {
		user_value.push($(this).val());
	});
	$('input[name="supername"]:checked').each(function() {
		super_value.push($(this).val());
	});
	$("#blockArr").val(block_value);
	$("#baseArr").val(base_value);
	$("#teamArr").val(team_value);
	$("#userArr").val(user_value);
	$("#superArr").val(super_value);

	$.cookies.set( 'blockCookie', block_value);
	$.cookies.set( 'baseCookie', base_value);
	$.cookies.set( 'teamCookie', team_value);
	$.cookies.set( 'userCookie', user_value);
	$.cookies.set( 'superCookie', super_value);
	//alert($.cookie.get('blockCookie'));
}
//全选反选
$(function() {
	//返回事件
	$("#back_return").click(function(){
		history.go(-1);
	});
    $("#cheeckAll").click(function() {
        if ($(this).is(":checked") == true) { //check all
            $("input[name='teamId']").each(function() {
                $(this).attr("checked", true);
            });
        } else {
            $("input[name='teamId']").each(function() {
                $(this).attr("checked", false);
            });

        }
    });
	/*
    $("#checkedBlockAll").click(function() {
        if ($(this).is(":checked") == true) { //check all
            $("input[name='blockname']").each(function() {
                $(this).attr("checked", true);
            });
        } else {
            $("input[name='blockname']").each(function() {
                $(this).attr("checked", false);
            });

        }
    });
    $("#checkedSuperAll").click(function() {
        if ($(this).is(":checked") == true) { //check all
            $("input[name='supername']").each(function() {
                $(this).attr("checked", true);
            });
        } else {
            $("input[name='supername']").each(function() {
                $(this).attr("checked", false);
            });

        }
    });

    $("#checkedBaseAll").click(function() {
        if ($(this).is(":checked") == true) { //check all
            $("input[name='basename']").each(function() {
                $(this).attr("checked", true);
            });
        } else {
            $("input[name='basename']").each(function() {
                $(this).attr("checked", false);
            });

        }
    });
    $("#checkedTeamAll").click(function() {
        if ($(this).is(":checked") == true) { //check all
            $("input[name='teamname']").each(function() {
                $(this).attr("checked", true);
            });
        } else {
            $("input[name='teamname']").each(function() {
                $(this).attr("checked", false);
            });

        }
    });
    $("#checkedUserAll").click(function() {
        if ($(this).is(":checked") == true) { //check all
            $("input[name='username']").each(function() {
                $(this).attr("checked", true);
            });
        } else {
            $("input[name='username']").each(function() {
                $(this).attr("checked", false);
            });

        }
    });
	*/
    $("#IbtnEnters").click(function() {
        var state = true;
        var mailTitle = $("#mailTitle").val();
        var mailContent = $("#mailContent").val();
		if ($('.checkbox').is(':checked') == true) {
            var allValue = [];
            $('input[name="sendAllUser"]:checked').each(function() {
                allValue.push($(this).val());
            });
		    $("#sendAllVal").val(allValue);
		}
		if ($('.cb').is(':checked') == true) {
      		valueCheck();
			state = true;
		} 
		if($('.cb').is(':checked') == false && $('.checkbox').is(':checked') == false){
            alert('请选择需要发送的对象!');
            state = false;
            return false;
        }
        if (mailTitle == "") {
            alert('请输入信件标题！');
            $('#mailTitle').focus();
            state = false;
            return false;
        }
        if (mailContent == "") {
            alert('请输入信件内容!');
            $('#mailContent').focus();
            state = false;
            return false;
        }
        if (state) {
			
            $('form').submit();
        }
    });

});

//清除cookie
function delCookie(){
$.cookies.set('blockCookie',null);  
$.cookies.set('baseCookie',null); 
$.cookies.set('teamCookie',null); 
$.cookies.set('userCookie',null); 
$.cookies.set('superCookie',null); 

}


//获取 cookie
function getCookie(coolietype){
	var blockCookie = $.cookies.get( 'blockCookie' );
	var baseCookie = $.cookies.get( 'baseCookie' );
	var teamCookie = $.cookies.get( 'teamCookie' );
	var userCookie = $.cookies.get( 'userCookie' );
	var superCookie = $.cookies.get( 'superCookie' );
	
	var  cookieIds='' ;
	str = ',';
	
	if(coolietype == 'supername'){	
		cookieIds = superCookie;
	}
	if(coolietype == 'teamname'){	
		cookieIds = teamCookie;
	}
	if(coolietype == 'blockname'){	
		cookieIds = blockCookie;
	}
	if(coolietype == 'username'){	
		cookieIds = userCookie;
	}
	if(coolietype == 'basename'){	
		cookieIds = baseCookie;
	}
	return cookieIds+str;

}
//地图显示
function mapShow(sX, sY , title) {
    var map = new BMap.Map("allmap"); // 创建Map实例
    var point = new BMap.Point(sX, sY); // 创建点坐标
    map.centerAndZoom(point, 15); // 初始化地图,设置中心点坐标和地图级别。
    map.enableScrollWheelZoom(); //启用滚轮放大缩小，默认禁用。 
    var marker = new BMap.Marker(point); // 创建标注
    map.addOverlay(marker); // 将标注添加到地图中
    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
    var label = new BMap.Label(title, {
        offset: new BMap.Size(20, -10)
    }); //标识提示文字
    marker.setLabel(label);

    map.addEventListener('click',
    function(e) {
        var info = new BMap.InfoWindow('', {
            width: 260
        });

        var lngLat = e.point;
        var lngLatStr = "经纬度：" + lngLat.lng + ", " + lngLat.lat;
        info.setContent(lngLatStr);
        if (window.confirm('您确定定位到此处吗？')) {
            map.clearOverlays(); //清除原来标记
            map.openInfoWindow(info, lngLat);
            var marker = new BMap.Marker(lngLat);
            map.addOverlay(marker);
            map.panTo(lngLat); //将地图的中心点更改为给定的点
            $("#lat").val(lngLat.lat); //重新赋值
            $("#lng").val(lngLat.lng);
            $("#latAlong").val(lngLat.lng + "," + lngLat.lat);
            // hidediv();
        } else {
            return false;
        }

    });

}
//显示隐藏地图
function hidediv() {
    document.getElementById("maptitle").style.display = 'none';
    document.getElementById("map").style.display = 'none';
}
//下拉框显示隐藏
function selectShowText() {
    $("#affiliation  option:selected").val() == "other" ? $("#affiliation_none").show() : $("#affiliation_none").hide()
}
function userselectShowText(){
    $("#skill  option:selected").val() == "其他" ? $("#skill_one").show() : $("#skill_one").hide()
}

function editInfo(m,id){
	location.href = "add"+m+"?id="+id;
}
